{{/*
Expand the name of the chart.
*/}}
{{- define "dbep-app.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
*/}}
{{- define "dbep-app.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "dbep-app.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Common labels
*/}}
{{- define "dbep-app.labels" -}}
helm.sh/chart: {{ include "dbep-app.chart" . }}
{{ include "dbep-app.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{/*
Selector labels
*/}}
{{- define "dbep-app.selectorLabels" -}}
app: {{ include "dbep-app.name" . }}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "dbep-app.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "dbep-app.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end -}}

{{/*
Image address
*/}}
{{- define "getImageDetails" -}}
{{- $namespace := .Release.Namespace -}}
{{- $bussEnvConfig := (lookup "v1" "ConfigMap" $namespace "buss-env-config") -}}
{{- if not $bussEnvConfig }}
  {{- fail (printf "ConfigMap 'buss-env-config' not found in namespace '%s'" $namespace) }}
{{- end }}
{{- if not $bussEnvConfig.data }}
  {{- fail "Data in 'buss-env-config' ConfigMap is missing." }}
{{- end }}
{{- $imageRepository := .Values.image.repository }}
{{- $imageTag := .Values.image.tag }}
{{- $imageDetails := printf "%s/%s/%s:%s" $bussEnvConfig.data.ZCM_HARBOR $bussEnvConfig.data.ZCM_HARBOR_PROJECT $imageRepository $imageTag }}
{{- $imageDetails }}
{{- end }}

{{- define "getEnv" -}}
{{- $namespace := .context.Release.Namespace -}}
{{- $host := .host -}}
{{- if not $host }}
  {{- "" -}}
{{- else }}
  {{- $bussEnvConfig := (lookup "v1" "ConfigMap" $namespace "buss-env-config") -}}
  {{- if not $bussEnvConfig }}
    {{- fail (printf "ConfigMap 'buss-env-config' not found in namespace '%s'" $namespace) }}
  {{- end }}
  {{- if not $bussEnvConfig.data }}
    {{- fail "Data in 'buss-env-config' ConfigMap is missing." }}
  {{- end }}
  {{- $envValue := (index $bussEnvConfig.data $host) -}}
  {{- if eq $envValue nil }}
    {{- fail (printf "Key '%s' not found in 'buss-env-config' ConfigMap." $host) }}
  {{- else }}
    {{- $envValue | default "" }}
  {{- end }}
{{- end }}
{{- end }}

{{- define "getNodeSelector" -}}
{{- $namespace := .Release.Namespace -}}
{{- $bussEnvConfig := (lookup "v1" "ConfigMap" $namespace "buss-env-config") -}}
{{- if not $bussEnvConfig }}
  {{- fail (printf "ConfigMap 'buss-env-config' not found in namespace '%s'" $namespace) }}
{{- end }}
{{- if not $bussEnvConfig.data }}
  {{- fail "Data in 'buss-env-config' ConfigMap is missing." }}
{{- end }}
{{- $zoneName := $bussEnvConfig.data.ZONE_NAME }}
{{- $zoneValue := $bussEnvConfig.data.ZONE_VALUE }}
{{- if not $zoneName }}
  {{- fail "ZONE_NAME is missing in 'buss-env-config' ConfigMap." }}
{{- end }}
{{- if not $zoneValue }}
  {{- fail "ZONE_VALUE is missing in 'buss-env-config' ConfigMap." }}
{{- end }}
{{- dict $zoneName $zoneValue | toYaml }}
{{- end }}

{{- define "shouldMountZLogs" -}}
{{- $namespace := .Release.Namespace -}}
{{- $bussEnvConfig := (lookup "v1" "ConfigMap" $namespace "buss-env-config") -}}
{{- if not $bussEnvConfig }}
  {{- fail (printf "ConfigMap 'buss-env-config' not found in namespace '%s'" $namespace) }}
{{- end }}
{{- if not $bussEnvConfig.data }}
  {{- fail "Data in 'buss-env-config' ConfigMap is missing." }}
{{- end }}
{{- $mountZLogs := $bussEnvConfig.data.MOUNT_ZLOGS | default "false" | lower }}
{{- if eq $mountZLogs "true" -}}
true
{{- else -}}
false
{{- end -}}
{{- end -}}
